package com.duoec.dw.lineage.service.analyser.expr;

import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
import com.duoec.dw.lineage.core.utils.AnalyserUtils;
import com.duoec.dw.lineage.dto.MultiTableLineageDto;
import com.duoec.dw.lineage.dto.TableFieldDependencyDto;
import org.springframework.stereotype.Service;

import java.util.LinkedHashSet;
import java.util.List;

/**
 * @author xuwenzhen
 */
@Service
public class MethodInvokeExprAnalyser extends BaseExprAnalyser<SQLMethodInvokeExpr> {
    /**
     * 获取匹配类型
     *
     * @return 类型
     */
    @Override
    public Class<SQLMethodInvokeExpr> getSqlExprType() {
        return SQLMethodInvokeExpr.class;
    }

    /**
     * 分析表达式语句
     *
     * @param expr         表达式语句
     * @param fromLineages 当前语句from解析出来的血缘关系
     * @return 此表达式抽取出来的依赖
     */
    @Override
    public LinkedHashSet<TableFieldDependencyDto> analyse(SQLMethodInvokeExpr expr, MultiTableLineageDto fromLineages) {
        LinkedHashSet<TableFieldDependencyDto> dependencies = new LinkedHashSet<>();
        List<SQLExpr> args = expr.getArguments();
        args.forEach(arg -> {
            LinkedHashSet<TableFieldDependencyDto> dependencies1 = hiveAnalyser.analyseExpr(fromLineages, arg);
            AnalyserUtils.mergeToDependencies(dependencies, dependencies1);
        });
        return dependencies;
    }
}
